package com.xingkeduo.business.operation.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xingkeduo.business.operation.controller.InitController;
import com.xingkeduo.business.operation.domain.CitiesDomain;
import com.xingkeduo.business.operation.domain.ServprodDomain;
import com.xingkeduo.pojo.json.ServprodJson;
import com.xingkeduo.pojo.query.CitiesQuery;
import com.xingkeduo.pojo.query.ServprodQuery;
import com.xingkeduo.pojo.table.CitiesTable;
import com.xingkeduo.pojo.table.ServprodTable;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.API_URL_PREFIX;

@RestController
@RequestMapping(API_URL_PREFIX +"/servprod")
public class ServprodApiController extends InitController {

    @Autowired
    private ServprodDomain servprodDomain;


    /**
     * id查询服务详情
     * @param
     * @return
     */
    @GetMapping(value = "/getServprodDetail")
    public ResponseUtils doGet(Long id) {
        returnUtil = new ReturnUtil();
        try {
            ServprodTable servprodTable = servprodDomain.getServprodById(id);
            return ResponseUtils.getSuccess(servprodTable);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }




    @RequestMapping(value = "/lists/{urlParam:.+}", method = RequestMethod.GET)
    public ResponseUtils doGetLists(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            ServprodQuery servprodQuery = new ServprodQuery();
            List<String> list = new ArrayList<>(10);
            if (urlParam.contains("[")) {
                JSONArray array = JSONArray.parseArray(urlParam);
                for (int i = 0; i < array.size(); i++) {
                    String string = array.getString(i);
                    list.add(string);
                }
            }
            if (list.size() > 0) {
                servprodQuery.setIds(list);
            }else{
                servprodQuery = JSON.parseObject(urlParam, ServprodQuery.class);
            }

            List<ServprodTable> servprodList = servprodDomain.getServprodList(servprodQuery);

            return ResponseUtils.getSuccess(servprodList);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }
}
